package codebot.processor.languages;

import java.util.Arrays;

import codebot.processor.IScreen;
import codebot.processor.logic.State;

/**
 * Specific syntax processor for Java
 */
public class CFamily extends Lang {

	protected static int DEPTH = Lang.DEPTH + 1;

	// Language specifics
	protected final static String[] FAMILY_KEYWORDS = new String[] { "if",
			"else", "return", "for", "while", "do", "switch", "case", "break",
			"continue", "default", "void", "int", "double" };
	protected static String[] KEYWORDS;

	// parameters
	protected final static String COLOR_KEYWORD = "rgb(127,0,145)";

	/**
	 * @see Lang.java
	 */
	@Override
	public void processSpecificSyntax(IScreen screen, String code, int s, int e) {
		final int codeStart = s - 3; // TODO correct this -3 correction
		// :) Wave's fault
		int i = 0;
		State state = new State(screen, codeStart);
		while (i < code.length()) {
			switch (state.getState()) {
			case 0:
				if (Character.isJavaIdentifierStart(code.charAt(i))) {
					state.set(1, i, null, null, null, null, null);
				}
				break;
			case 1:
				switch (code.charAt(i)) {
				case ' ':
				case '\n':
				case '(':
					state.set(0, null, i, null, null, null, null);
					String word = code.substring(state.getStart(), state
							.getEnd());
					if (Arrays.asList(KEYWORDS).contains(word)
							|| Arrays.asList(FAMILY_KEYWORDS).contains(word)) {
						state.set(null, null, null, null, COLOR_KEYWORD, null,
								null);
					}
					break;
				}
				break;
			}
			i++;
		}
	}

	@Override
	public String toString() {
		return "CFamily";
	}

}
